# -*- coding: utf-8 -*-
import pymongo
client = pymongo.MongoClient('127.0.0.1')
db = client.city4
import re
import MySQLdb
import MySQLdb.cursors
import pymongo
conn = MySQLdb.connect("127.0.0.1","root","123456789", "shantianjun",charset="utf8mb4",cursorclass=MySQLdb.cursors.DictCursor)
sql = "select * from store"
cursor = conn.cursor()
cursor.execute(sql)
stores = cursor.fetchallDict()
data = {}
urlpool = set()
for y in db.girl.find():
    # import ipdb;ipdb.set_trace()
    data = {}
    try:
        # data["workTime"] = re.search("\d",y["workTime"]).group(1) if re.search("d",y["workTime"]) else ""
        if y["girlUrl"] in urlpool:
            continue
        if re.search("\d+",y["age"]):
            data["age"] = re.search("\d+",y["age"]).group()
        data["name"] = y["name"]
        bwh = re.findall("\d+",y["bwh"])
        if not y["blood"] and not y["bwh"] and not y["age"]:
            continue
        if len(bwh) > 0:
            try:
                data["height"] = bwh[0] or ""
                data["bust"] = bwh[1] or ""
                data["waist"] = bwh[2] or ""
                data["hipline"] = bwh[3] or ""
            except Exception, e:
                pass
        if re.search(r"\((.+)\)",y["bwh"]):
            data["cup"] = re.search(r"\((.+)\)",y["bwh"]).groups()[0]
        # data["bwh"] = y["bwh"]
        data["blood"] = y["blood"]
        # data["point"] = ",".join(y["point"]) if type(y["point"]) == list else y["point"]
        # data["questions"] = ",".join(y["questions"]) if type(y["questions"]) == list else y["questions"]
        # data["disposition"] = ",".join(y["character"]) if type(y["character"]) == list else y["character"]
        # data["review"] = y["storeComment"]
        # data["comment"] = y["comment"]
        if len(y["constellation"]) > 11 and not data["blood"]:
            data["constellation"] = re.search(u'(\S+)\u5ea7\s+(\S+)\u578b',y["constellation"]).groups()[0]
            data["blood"] = re.search(u'(\S+)\u5ea7\s+(\S+)\u578b',y["constellation"]).groups()[1]
        else:
            data["constellation"] = y["constellation"]
        tmp = y.get("image_urls")
        # if y.get("image_urls"):
        #     for x in y.get("image_urls"):
        #         result = client.dmm.img.find_one({"type":"citygirl","source":x})
        #     if result:
        #         tmp.append(result.get("path"))
        data["imgs"] = ",".join(tmp)
        data["thumb"] = tmp[0]
        data["url"] = y["girlUrl"].replace("https://www.cityheaven.net","")
        for yy in stores:
            if yy["url"] == y["storesUrl"].replace("https://www.cityheaven.net",""):
                data['sid'] = yy["id"]
        # import ipdb;ipdb.set_trace()

        fields = tuple(data.keys())
        fields = str(tuple(fields)).replace("'",'')
        data_dict = data.values()
        data_values = "(" + "%s," * (len(data_dict)) + ")"
        data_values = data_values.replace(',)', ')')

        sql = "insert into girl %s values %s" % (fields,data_values)
        cursor.execute(sql,tuple(data_dict))
        urlpool.add(y["girlUrl"])
        print sql
    except Exception, e:
        print e
        import ipdb;ipdb.set_trace()
cursor.close()
conn.commit()
# insert into cate(name) select distinct(cate) from store where store.cate <> ""
# insert into mode(name) select distinct(mode) from store where store.mode  != "";
# insert into girlDetail (gid,comment,review,disposition,questions,point) select id,comment,review,disposition,questions,point from girl
# insert into girlDetail (id,imgs,sid) select id,imgs,sid from girl
